#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年5月25日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

sys.path.append('../')
from base.localConfig import baseLocalConfig



import logging


import datetime

from base.PyMySQL import ConnectMySQL
from base.time_method import myTimeMethod

zhimind_host = baseLocalConfig.mysqlConfig['zhimind']['host']
zhimind_port = baseLocalConfig.mysqlConfig['zhimind']['port']
zhimind_user = baseLocalConfig.mysqlConfig['zhimind']['user']
zhimind_passwd = baseLocalConfig.mysqlConfig['zhimind']['passwd']
zhimind_db = baseLocalConfig.mysqlConfig['zhimind']['db']
myZhimind = ConnectMySQL(host=zhimind_host, user=zhimind_user, password=zhimind_passwd, db=zhimind_db)





logging.basicConfig(level=logging.INFO)


import argparse,re,datetime,time
parser = argparse.ArgumentParser(description='args')
parser.add_argument('--startTime',metavar=u"starttime",default=None)
parser.add_argument('--endTime',metavar=u"endtime",default=None)

args = parser.parse_args()
startTime = args.startTime 
endTime = args.endTime
if startTime!=None:
    st = startTime
else:
    st = '2015-06-01'
if endTime !=None:
    et = endTime
else:
    et = '2015-07-01'

days = myTimeMethod.get_slist_between_st_et(st, et)

st = '2014-09-01'
for day in days:
    if int(day.split('-')[-1]) not in (1,15):continue
    print 'start run %s'%day
#     continue
    #全部
    
    #-- 非独立mac数

#     sql = 'select sum(visit_freq) from ( \
#            select sum(visit_freq) as visit_freq from mac_customer_stats where day between "%s" and "%s" \
#             union all \
#             select sum(visit_freq) as visit_freq from mac_walkby_stats where day between "%s" and "%s") as tab'%(st,day,st,day)
#     logging.debug(sql)
#     print 'all:非独立mac:',myZhimind.SelectOne(sql)[0],
#     #-- 独立mac数
#     sql = 'select count(distinct mac) from \
#             (select mac from mac_customer_stats where day between "%s" and "%s" \
#             union all \
#             select mac from mac_walkby_stats where day between "%s" and "%s") as tab'%(st,day,st,day)
#     
#     logging.debug(sql)
#     #print 'all:独立mac:',myZhimind.SelectCount(sql),
#     
#     print 'all:独立mac:',''
#     #顾客
#     sql = 'select count(distinct mac) from mac_customer_stats where day between "%s" and "%s"'%(st,day)
#     logging.debug(sql)
#     print 'customer:独立mac:',myZhimind.SelectCount(sql)
#     #
# 
#     continue
    # -- 按月提供  独立mac数

    if day.split('-')[-1] == '01':
        sql = 'select count(distinct mac) from (\
            select mac from mac_customer_stats where day>="%s" and day<"%s" \
            union all \
            select mac from mac_walkby_stats where day>="%s" and day<"%s" \
            ) as tab'%(st,day,st,day)
        
    # -- 按月提供 非独立mac数
    if day==et:

        
        sql = 'select month(day) as month,sum(visit_freq) from (\
                select day,sum(visit_freq) as visit_freq from mac_customer_stats where day between "%s" and "%s" \
                union all \
                select day,sum(visit_freq) as visit_freq from mac_walkby_stats where day between "%s" and "%s" \
                ) as tab group by month'%(st,et,st,et) 
        
        sql1 = 'select month(day) as month,sum(visit_freq) as visit_freq from mac_customer_stats where day between "%s" and "%s" group by month;'%(st,et)
        print sql1
        

